package com.jmu.Shape;//
import java.util.Objects;
import java.util.Scanner;

public class Triangle extends Shape {
    //设置三条边长
    double length1;
    double length2;
    double length3;

    public Triangle(double a, double b, double c) {
        this.length1 = a;
        this.length2 = b;
        this.length3 = c;
        //用递归判断是否能构成三角形 或者 重新设置长度
        if (judge(a, b, c)) {
            reSet();
        }
    }

    /**
     * 计算三角形周长
     * @return
     */
    @Override
    public double getPerimeter() {
        return length1 + length2 + length3;
    }

    /**
     * 海伦公式计算三角形面积
     * @return
     */
    @Override
    public double getArea() {
        double result = 0;
        double p = (length1 + length2 + length3) / 2;
        result = Math.sqrt(p * (p-length1) * (p-length2) * (p-length3));
        return result;

    }

    /**
     * 判断三边是否能构造成三角形 不可以返回true
     * @param a
     * @param b
     * @param c
     * @return result
     */
    private boolean judge(double a, double b, double c) {
        boolean result = false;
        if ((a + b) <= c || (a + c) <= b || (b + c) <= a)
            result = true;
        return result;
    }

    /**
     * 重新设置三角形的三条边
     */
    private void reSet() {
        System.out.println("三边无法构建出三角形,请重新设置：");
        Scanner scanner = new Scanner(System.in);
        this.length1 = scanner.nextInt();
        this.length2 = scanner.nextInt();
        this.length3 = scanner.nextInt();
        if (judge(this.length1, this.length2, this.length3))
            reSet();
    }


    //setter/getter方法
    public double getLength1() {
        return length1;
    }

    public double getLength2() {
        return length2;
    }

    public double getLength3() {
        return length3;
    }

    public void setLength1(double length1) {
        this.length1 = length1;
    }

    public void setLength2(double length2) {
        this.length2 = length2;
    }

    public void setLength3(double length3) {
        this.length3 = length3;
    }

    //toString
    @Override
    public String toString() {
        return "Triangle{" +
                "length1=" + length1 +
                ", length2=" + length2 +
                ", length3=" + length3 +
                '}';
    }

    //equals
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Triangle triangle = (Triangle) o;
        return Double.compare(triangle.length1, length1) == 0 &&
                Double.compare(triangle.length2, length2) == 0 &&
                Double.compare(triangle.length3, length3) == 0;
    }

    //hashCode
    @Override
    public int hashCode() {
        return Objects.hash(length1, length2, length3);
    }
}
